package com.kmmlearn.findtime.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.kmmlearn.findtime.TimeZoneHelper
import com.kmmlearn.findtime.TimeZoneHelperImpl
import com.kmmlearn.findtime.ui.components.AnimatedSwipeDismiss
import kotlinx.coroutines.delay

const val timeMillis = 1000 * 60L   // 1 minute

@Composable
fun TimeZoneScreen(currentTimezoneStrings: SnapshotStateList<String>) {
    val timeZoneHelper: TimeZoneHelper = TimeZoneHelperImpl()
    val listState = rememberLazyListState()
    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        var time by remember { mutableStateOf(timeZoneHelper.currentTime()) }
        // Use Compose’s LaunchedEffect . It will be launched once but continue to run.
        // 传入0，重组时不会重新启动
        LaunchedEffect(0) {
            while (true) {
                time = timeZoneHelper.currentTime()
                delay(timeMillis)
            }
        }
        LocalTimeCard(
            city = timeZoneHelper.currentTimeZone(),
            time = time,
            date = timeZoneHelper.getDate(timeZoneHelper.currentTimeZone())
        )
//        Spacer(modifier = Modifier.weight(1.0f))

        LazyColumn(
            state = listState
        ) {
            items(currentTimezoneStrings, key = { timezone -> timezone }) { timezoneString ->
                AnimatedSwipeDismiss(
                    item = timezoneString,
                    background = {
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .height(50.dp)
                                .background(Color.Red)
                                .padding(
                                    start = 20.dp,
                                    end = 20.dp
                                )
                        ) {
                            val alpha = 1f
                            Icon(
                                Icons.Filled.Delete,
                                contentDescription = "Delete",
                                modifier = Modifier.align(Alignment.CenterEnd),
                                tint = Color.White.copy(alpha = alpha)
                            )
                        }
                    },
                    content = {
                        TimeCard(
                            timezone = timezoneString,
                            hours = timeZoneHelper.hoursFromTimeZone(timezoneString),
                            time = timeZoneHelper.getTime(timezoneString),
                            date = timeZoneHelper.getDate(timezoneString)
                        )
                    },
                    onDismiss = { zone ->
                        if (currentTimezoneStrings.contains(zone)) {
                            currentTimezoneStrings.remove(zone)
                        }
                    }
                )
            }

        }
    }
}